package com.qf.dao.impl;

import com.qf.dao.GoodsTypeDao;
import com.qf.domain.GoodsType;
import com.qf.utils.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class GoodsTypeDaoImpl implements GoodsTypeDao {
    private QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
    @Override
    public List<GoodsType> select(int level) {
        String sql = " select id,name,level,parent from tb_goods_type where level = ? ";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(GoodsType.class),level);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public GoodsType selectById(Integer typeId) {
        String sql = "select * from tb_goods_type where id = ? ";
        try {
            return queryRunner.query(sql,new BeanHandler<>(GoodsType.class),typeId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<String> selectName() {
        String sql = "SELECT `name` FROM `tb_goods_type` WHERE `level`=1; ";
        try {
            List<String> query =  queryRunner.query(sql,new ColumnListHandler<>());
            return query;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<GoodsType> findAll()  {
        try {
            return queryRunner.query("select * from tb_goods_type",new BeanListHandler<>(GoodsType.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void insert(GoodsType goodsType)  {
        String sql="insert into tb_goods_type (name,level,parent) values(?,?,?)";

        try {
            queryRunner.update(sql,goodsType.getName(),goodsType.getLevel(),goodsType.getParent());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<GoodsType> selectByNAL(StringBuilder where1, List<Object> i)  {
        String sql="select * from tb_goods_type " +where1;
        try {
            List<GoodsType> goodsTypes = queryRunner.query(sql, new BeanListHandler<>(GoodsType.class), i.toArray());
            return goodsTypes;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void update(GoodsType goodsType)  {
        String sql="update tb_goods_type set name=?,level=?,parent=? where id=?";
        Object []param={goodsType.getName(),goodsType.getLevel(),goodsType.getParent(),goodsType.getId()};
        try {
            queryRunner.update(sql,param);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
